<?php

class Chung_chisController extends BaseController {

    /**
     * Chung_chi Repository
     *
     * @var Chung_chi
     */
    protected $chung_chi;

    public function __construct(Chung_chi $chung_chi) {
        $this->chung_chi = $chung_chi;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $chung_chis = $this->chung_chi->all();

        return View::make('chung_chis.index', compact('chung_chis'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('chung_chis.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $input = Input::all();
        $validation = Validator::make($input, Chung_chi::$rules, Chung_chi::$messages);

        if ($validation->passes()) {
            $this->chung_chi->create($input);

            return Redirect::route('certificates.index');
        }

        return Redirect::route('certificates.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $chung_chi = $this->chung_chi->findOrFail($id);

        return View::make('chung_chis.show', compact('chung_chi'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $chung_chi = $this->chung_chi->find($id);

        if (is_null($chung_chi)) {
            return Redirect::route('certificates.index');
        }

        return View::make('chung_chis.edit', compact('chung_chi'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {

        $input = array_except(Input::all(), '_method');
        $chung_chi = $this->chung_chi->findOrFail($id);
        $current_chung_chi = $chung_chi['ma_chung_chi'];
        if($current_chung_chi == $input['ma_chung_chi']){
            $chung_chi = $this->chung_chi->find($id);
            $chung_chi->update(
                array('ten_chung_chi'=>$input['ten_chung_chi'])
                );
            return Redirect::route('certificates.show', $id);
        }else{
            $validation = Validator::make($input, Chung_chi::$rules);

            if ($validation->passes()) {
                $chung_chi = $this->chung_chi->find($id);
                $chung_chi->update($input);

                return Redirect::route('certificates.show', $id);
            }
            return Redirect::route('certificates.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
        }
 
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->chung_chi->find($id)->delete();
        return 1;
        // return Redirect::route('certificates.index');
    }

}
